package com.mi.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mi.common.core.util.Result;
import com.mi.common.log.annotation.Log;
import com.mi.system.dto.query.RoleQueryDTO;
import com.mi.system.dto.request.RoleRequestDTO;
import com.mi.system.entity.Role;
import com.mi.system.service.RoleService;
import com.mi.system.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.mi.common.core.util.Result.ok;
import static com.mi.common.log.constant.LogConstants.SERVICE_ID_USER;

/**
 * Role controller
 *
 * @author jin_huaquan
 * @version 1.0
 * @date 2021/11/1 15:09
 */
@Api(value = "角色API", tags = "Role")
@ApiSort(998)
@RestController
@RequestMapping("/api/v1/roles")
@RequiredArgsConstructor
@Slf4j
public class RoleController {

    private final RoleService roleService;

    @ApiOperation(value = "分页查询角色列表", notes = "分页查询角色列表")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 1)
    @ApiImplicitParam(name = "name", value = "角色的名称", paramType = "query", dataTypeClass = String.class)
    @Log(title = "查询角色列表", serviceId = SERVICE_ID_USER)
    @GetMapping("/actions/list")
    public Result<IPage<RoleVO>> listRole(Page page, RoleQueryDTO query) {
        log.info("API CALL : list role.");

        return ok(roleService.listRole(page, query));
    }

    @ApiOperation(value = "查询角色列表", notes = "查询角色列表")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 2)
    @ApiImplicitParam(name = "name", value = "角色的名称", paramType = "query", dataTypeClass = String.class)
    @Log(title = "罗列角色", serviceId = SERVICE_ID_USER)
    @GetMapping("/actions/query")
    public Result<List<RoleVO>> listRole(RoleQueryDTO query) {
        log.debug("API CALL : query role.");

        return ok(roleService.listRole(query));
    }

    @ApiOperation(value = "保存角色", notes = "保存角色")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 3)
    @Log(title = "新增角色:#{#request.name}", serviceId = SERVICE_ID_USER)
    @PostMapping
    public Result<Boolean> saveRole(@Valid @RequestBody RoleRequestDTO request) {
        log.debug("API CALL : save role.");

        return ok(roleService.saveRole(request));
    }

    @ApiOperation(value = "更新角色", notes = "更新角色信息")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 4)
    @ApiImplicitParam(name = "id", value = "角色的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "编辑角色:#{#request.name}", serviceId = SERVICE_ID_USER)
    @PutMapping("/{id}")
    public Result<Boolean> updateRole(@PathVariable Long id, @Valid @RequestBody RoleRequestDTO request) {
        log.debug("API CALL : update role.");

        return ok(roleService.updateRole(id, request));
    }

    @ApiOperation(value = "删除角色", notes = "删除角色")
    @ApiOperationSupport(author = "jin_huaquan@finsung.com", order = 5)
    @ApiImplicitParam(name = "id", value = "角色的ID", paramType = "path", dataType = "java.lang.String", required = true)
    @Log(title = "删除角色:#{#request.name}", serviceId = SERVICE_ID_USER)
    @DeleteMapping("/{id}")
    public Result<Role> removeRole(@PathVariable Long id) {
        log.debug("API CALL : remove role.");

        return ok(roleService.removeRole(id));
    }
}
